﻿package {
	import flash.events.TimerEvent;
	import flash.utils.Timer;	 
	import org.flixel.*;
	
	public class Threat extends Enemy {
		private var _shot_timer:Timer;
		
		public function Threat(state:int,from:int,x:int,y:int) {
			super(state, from, x, y);

			loadGraphic(ResourceManager.enemy1, true);
			addAnimation("sorted_colors", [FlxU.random()*5], 20, true);
			play("sorted_colors");

			health = 10;

			_shot_velocity.x = 0;
			_shot_velocity.y = 40;
			
			_default_velocity = 30;
            _max_bullets = 5;
			velocity.y = _default_velocity;			
					
            var s:FlxSprite;
            for(var i:int = 0;i <_max_bullets;i++) {
                var shot:ShotSingle = new ShotSingle(this,(x + (width/2)-4), y+20);
                _bullets.add(shot);
            }

			/* timer for spit fire at 1 secs */
			_shot_timer = new Timer(1000, 0);
			_shot_timer.addEventListener(TimerEvent.TIMER, shotTimer);
			_shot_timer.start();
			shotTimer();

            FlxG.log("Threat:constructor");
		}

		private function shotTimer(event:TimerEvent = null):void {
            FlxG.log("Threat:shotTimer");
			if (_curr_state == attack_state || _curr_state == waiting_state) {
                FlxG.log("Yes we shot");
                var b:FlxSprite = _bullets.members[_bullets_index];
                b.reset(x + (width/2) -4,y+20);
                FlxG.state.add(b);
				b.velocity.y += _shot_velocity.y;
				b.velocity.x += _shot_velocity.x;
                StatePlay(FlxG.state)._enemyShot.add(b);
                _bullets_index++;
                if(_bullets_index >= _bullets.members.length)
                    _bullets_index = 0;
			}
		}
		
		override protected function onAttackState():void {
			super.onAttackState();
			switch(_curr_from) {
				case from_left:	  velocity.x = 10;  break;
				case from_right:  velocity.x = -10; break;
				case from_center: velocity.x = 0;   break;
				default: velocity.x = 0; break;
			}
		}
		
		override protected function onRunawayState():void {
			super.onRunawayState();
			switch(_curr_from) {
				case from_left:	  velocity.x = 100;  break;
				case from_right:  velocity.x = -100; break;
				case from_center: velocity.x = 0;   break;
				default: velocity.x = 0; break;
			}
		}
		
		override protected function onWaitingState():void {
			super.onWaitingState();
			velocity.y = 0;
			velocity.x = 0;
		}
		
		override public function kill():void {
            FlxG.log("Threat:kill");
			FlxG.play(ResourceManager.enemy_dies_1);
			
			_shot_timer.removeEventListener(TimerEvent.TIMER, shotTimer);
			_shot_timer.stop();
			
			//TODO: improve position of explode
			var ex:ExplosionMinor = new ExplosionMinor();
			FlxG.state.add(ex);
			ex.explode(x - (width / 2), y - (height / 2));
			super.kill();
		}
		
		override public function update():void {
			super.update();
		}
		
		override public function destroy():void {
			/* we must unregister that or problems will occur */
			_shot_timer.removeEventListener(TimerEvent.TIMER, shotTimer);
			_shot_timer.stop();
			
			super.destroy();
		}
	}
}
